//
//  ShareCell.swift
//  PandaSwift
//
//  Created by 余意 on 2017/2/20.
//  Copyright © 2017年 余意. All rights reserved.
//

import UIKit
import Kingfisher


class ShareCell: UITableViewCell {
    
    
    var IV : UIImageView?
    var titleLab : UILabel?
    var describeLab : UILabel?
    var IVString : String?
    
    var modelStore : ShareModel!
    
    var model : ShareModel
        {
        set{
            self.modelStore = newValue
            self.titleLab?.text = self.modelStore.title
            self.describeLab?.text = self.modelStore.description
            
            if let string = self.modelStore.cover
            {
                if string  == ""
                {
                    if let stringtmp = self.modelStore.images?[0]["url"]
                    {
                        self.IVString = stringtmp
                    }
                }
                else
                {
                    self.IVString = string
                }
            }
            else
            {
                if let stringtmp = self.modelStore.images?[0]["url"]
                {
                   self.IVString = stringtmp
                }
            }
            
            let url = URL(string:self.IVString!)
            self.IV?.kf.setImage(with: url,
                                 placeholder: nil,
                                 options: [.transition(.fade(1))],
                                 progressBlock: nil,
                                 completionHandler: nil)
            
        }
        get{
            return self.modelStore
        }
    }
    
    
    

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style , reuseIdentifier:reuseIdentifier)
        self.setup()
    }

    
    func setup()
    {
        self.IV = UIImageView.init()
//        self.IV?.backgroundColor = UIColor.red
        self.contentView.addSubview(self.IV!)
        self.IV?.snp.makeConstraints({ (make) in
            make.centerY.equalTo(self.contentView)
            make.left.equalTo(self.contentView).offset(10)
            make.top.equalTo(self.contentView).offset(8)
            make.width.equalTo(120)
        })
        
        self.titleLab = UILabel.init()
        self.titleLab?.numberOfLines = 1
        self.titleLab?.font = UIFont.systemFont(ofSize: 17)
        self.titleLab?.textColor = UIColor.black
        self.contentView.addSubview(self.titleLab!)
        self.titleLab?.snp.makeConstraints({ (make) in
            make.top.equalTo(self.contentView).offset(18)
            make.left.equalTo(self.IV!.snp.right).offset(10)
            make.right.equalTo(self.contentView).offset(-10)
        })
        
        self.describeLab = UILabel.init()
        self.describeLab?.numberOfLines = 2
        self.describeLab?.font = UIFont.systemFont(ofSize: 10)
        self.describeLab?.textColor = UIColor.gray;
        self.contentView.addSubview(self.describeLab!)
        self.describeLab?.snp.makeConstraints({ (make) in
            make.top.equalTo(self.titleLab!.snp.bottom).offset(10)
            make.left.equalTo(self.titleLab!)
            make.right.equalTo(self.titleLab!)
        })
        
    }

}
